
<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<% 
    if(session.getAttribute("Access").equals("Customer"))
    {
        
        Connection conn=null;
        String itemName = "";
        int numUnits = -1;
        try
        {
             conn = new DBClasses.GetConnection().getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT ItemName, AvailableUnits FROM advertisement WHERE AdvertisementID='"
                     + request.getParameter("AD") + "'");
             rs.next();

             itemName = rs.getString("ItemName");
             numUnits = rs.getInt("AvailableUnits");
             


        }
        catch(Exception e) { e.printStackTrace(); response.sendRedirect("gohome.jsp"); }

        finally
        {
            try{ conn.close(); }
            catch(Exception e) {}
        }
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Purchase Item</title>
<script type="text/javascript">
function on_purchase()
{
    if(document.purchase.itemamount.value > <%=numUnits%>)
    {
        alert("Cannot order that many units at this time!");
    }
    else
    {
        document.purchase.submit()
    }
}
</script>
</head>
    

<body>
<table width="500" border="0">
  <tr>
    <td><input type="button" value="Home" onclick="window.location='gohome.jsp'"/>&nbsp;
          <input type="button" value="Log Out" onclick="window.location='logout.jsp'"/></td>
  </tr>
  <tr>
    <td>Purchase Item: 
        <label><%=itemName%></label></td>
  </tr>
  <tr>
    <td><form name="purchase" action="purchase.jsp" method="post"> 
            <label>Enter Amount: </label>
            <input type="text" name="itemamount" value="1" />
            <input type="hidden" name="itemid" value="<%=request.getParameter("AD")%>" />
        </form></td>
  </tr>
  <tr>
    <td><input type="button" value="Purchase" onclick="on_purchase()"/>&nbsp;
    <input type="button" value="Cancel" onclick="window.location='AdvertisementList.jsp'"/></td>
  </tr>
  <tr>
    <td><h6>Note: your purchase will be saved in the system</h6></td>
  </tr>
</table>
      
</body>
<% } 
    else 
    {    
        Connection conn=null;
        try
        {
             session.setAttribute("AdID", request.getParameter("AD"));
             conn = new DBClasses.GetConnection().getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM advertisement WHERE AdvertisementID='"
                     + request.getParameter("AD") + "'");
             
             String itemName = "";
             String company = "";
             String itemType = "";
             String content = "";
             double unitPrice = -1;
             int availableUnits = -1;
             Date date = null;
             int employee = -1;
             Boolean active = true;
             
             if(rs.next())                    
             {
                 itemName = rs.getString("ItemName");
                 company = rs.getString("Company");
                 itemType = rs.getString("ItemType");
                 content = rs.getString("Content");
                 unitPrice = rs.getDouble("UnitPrice");
                 availableUnits = rs.getInt("AvailableUnits");
                 date = rs.getDate("Date");
                 employee = rs.getInt("Employee");
                 active = rs.getBoolean("Active");
                 
             }
             if(date==null)
                 date = new Date(System.currentTimeMillis());
             if(employee==-1)
                 employee = Integer.parseInt(session.getAttribute("EmployeeID").toString());

%>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Item</title>
</head>
<body>
<table width="500" border="0">
  <tr>
    <td><input type="button" value="Home" onclick="window.location='gohome.jsp'"/>&nbsp;
          <input type="button" value="Log Out" onclick="window.location='logout.jsp'"/></td>
  </tr>
    <tr><td align="right" style="padding-right: 200px;"><h2>Edit Item</h2></td></tr>
    <tr>
    <td align="right">
        <form style="padding-right: 200px;" name="sale" action="advertisement.jsp" method="post"> 
            <label>Item Name: </label>
            <input type="text" maxlength="50" name="ItemName" value="<%=itemName%>" /><br />
            
            <label>Company: </label>
            <input type="text" maxlength="50" name="Company" value="<%=company%>" /><br />
            
            <label>Item Type: </label>
            <input type="text" maxlength="50" name="ItemType" value="<%=itemType%>" /><br />
            
            <label>Content: </label>
            <input type="text" maxlength="50" name="Content" value="<%=content%>" /><br />
            
            <label>Unit Price: </label>
            <input type="text" name="UnitPrice" value="<%=unitPrice%>" /><br />
            
            <label>Available Units: </label>
            <input type="text" name="AvailableUnits" value="<%=availableUnits%>" /><br />
            
            <label>Date: </label>
            <input readonly="readonly" type="text" name="Date" value="<%=date%>" /><br />
            
            <label>Employee: </label>
            <input readonly="readonly" type="text" name="Employee" value="<%=employee%>" /><br />
            
            <select name="Active">
                <option value="1" <% if(active) { %> selected="selected" <% } %>>Active</option>
                <option value="0" <% if(!active) { %> selected="selected" <% } %>>Inactive</option>
            </select><br />
            
            
        </form>
    </td>
    </tr>
  <tr>
    <td align="right" style="padding-right: 200px;"><input type="button" value="Update" onclick="document.sale.submit()"/>&nbsp;
    <input type="button" value="Cancel" onclick="window.location='gohome.jsp'"/></td>
      
  </tr>
</table>
</body>
<%      }
        catch(Exception e) { e.printStackTrace(); response.sendRedirect("gohome.jsp"); }

        finally
        {
            try{ conn.close(); }
            catch(Exception e) {}
        }   
    } %>
</html>

